/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slisfx.screens;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import slisfx.Globals;
import slisfx.exception.SLISExceptionFX;
import slisfx.resources.Load;
import slisfx.user.Userbase;
import slisfx.xml.SAXReader;

/**
 *
 * @author Rebecky
 */
public class LoginScreenFX extends Application implements Globals  {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(final Stage primaryStage){
        /* Reading in from xml file
         * File name set in Globals.java
         */
        new SAXReader().saxReader();
        
      /*Sets the Title for the primary stage.*/
        primaryStage.setTitle("NASA SLIS Login");
        primaryStage.setResizable(false);
        
      /*Here we define all of our different boxes*/
        /*Creates our main Layout.
         * Sets the border to all black
         */
        BorderPane mainWindow = new BorderPane(); 
        mainWindow.setTop(new Rectangle(830,50, Color.BLACK));
        mainWindow.setBottom(new Rectangle(830,50, Color.BLACK));
        mainWindow.setLeft(new Rectangle(50,350, Color.BLACK));
        mainWindow.setRight(new Rectangle(50, 350, Color.BLACK));
        
        /*Creates our image box.
         * Sets the spacing for the images.
         */
        //Places the two images side by side.
        HBox imageBox = new HBox();
        imageBox.setPadding(new Insets(10, 10, 10, 150));
        imageBox.setSpacing(100);
        
        //used to create a background.
        StackPane stackPane = new StackPane();
        
        /*Creating a new vertical menu.
         * this stores the text and password fields.
         */
        VBox mainMenu = new VBox();
        mainMenu.setPadding(new Insets(10, 10, 10, 10));
        mainMenu.setSpacing(10);
        
        /*Creating a title for the mainWindow.
         * sets the font style and size.
         */
        Text title = new Text("Welcome to the NASA SLIS Project.");
        title.setFont(Font.font("Amble CN", FontWeight.BOLD, 18));
        title.setFill(Color.WHITE);
        
        /*Creating a textField
         * prompts the user to enter their user name.
         */
        final TextField uName = new TextField();
        uName.setPromptText("Enter your user name.");
        uName.setPrefColumnCount(10);
        
        /*Creating a password field
         * prompts the user to enter their password.
         */
        final PasswordField pb = new PasswordField();
        pb.setPromptText("Enter your password.");
        pb.setPrefColumnCount(10);
        
        /*Creating the login button. 
         * sets the actions when pushed.
         */
        Button loginButton = new Button("Login");
        loginButton.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event) {
                    int permission = Userbase.getUserbase().check_user(uName.getText(), pb.getText());
                    if(permission == ADMIN){
                        new AdminScreenViewFX().start(primaryStage);
                    }
                    else if(permission == USER){
                        new DeviceViewFX().start(primaryStage);
                    }
                    else if(permission == INCORRECT_USERNAME_PASSWORD){
                            SLISExceptionFX except = new SLISExceptionFX("Your username and or your password is incorrect.", LOGIN_SCREEN);
                            except.start(primaryStage);
                    }
                    else{
                        SLISExceptionFX except = new SLISExceptionFX("Incorrect permissions are set.", LOGIN_SCREEN);
                        except.start(primaryStage);
                    }
            }
        });
        
        /* Images used for imageBox
         * Nasa Logo and WVU Logo
         */
        //Image im = new Image("src/slisfx/resources/NASA_IVV1.png"); 
        Image im, im1, im2; 
        ImageView iv1 = new ImageView();
        ImageView iv2 = new ImageView();
        ImageView iv3 = new ImageView();
        try {
            im = new Image(new Load().loadFile("NASA_IVV1.png"));
            iv1.setImage(im);
                    
            im1 = new Image(new Load().loadFile("WVU_LOGO.png")); 
            iv2.setImage(im1);
        
            /* Images used for background
            * background defined in Globas.java
            */
            im2 = new Image(new Load().loadFile("Stars.png")); 
            iv3.setImage(im2);
        } catch (IOException ex) {
            Logger.getLogger(LoginScreenFX.class.getName()).log(Level.SEVERE, null, ex);
        }



        /*Image Box
         * adds WVU logo and NASA logo
         */
        imageBox.getChildren().add(iv1);
        imageBox.getChildren().add(iv2);

        /* Main Menu
         * adds all items to the main menu.
         */
        mainMenu.getChildren().add(title);
        mainMenu.getChildren().add(imageBox);    
        mainMenu.getChildren().add(uName);
        mainMenu.getChildren().add(pb);
        mainMenu.getChildren().add(loginButton);

        /* Stack Pane
         * Adds mainMenu on top of background.
         */
        stackPane.getChildren().addAll(iv3, mainMenu);
        
        /* Main Window
         * Adds Stack Pane to center.
         */
        mainWindow.setCenter(stackPane);     

        /*Sets the screen size and displays*/
        primaryStage.setScene(new Scene(mainWindow, WIDTH, HEIGHT));
        primaryStage.show();
    }

}
